﻿#ifndef DATAPACKAGE_UDPCODEC_H
#define DATAPACKAGE_UDPCODEC_H

#include <qobject.h>
/*
 *UdpCodec内部使用的数据接口类
*/
class QNetworkDatagram;
class DataPackage_UdpCodec :  public QObject
{
  Q_OBJECT
private:
  const static ushort maxDataSize=256;//最大单个数据包字节数
private://property
  uint senderAddress;
  ushort senderPort=0;
  uint receiverAddress;
  uint receiverPort=0;
  uint deviceId=0;
  uchar packageType=0;
  ushort msgId=0;
  ushort timeStamp=0;
  QByteArray data;
public:
  const static int dataHeadSize=sizeof (packageType)+sizeof (msgId)+sizeof (deviceId)+sizeof (timeStamp);
public:
  explicit DataPackage_UdpCodec();
  DataPackage_UdpCodec(const DataPackage_UdpCodec& dataPackageInfo);
  DataPackage_UdpCodec &operator=(const DataPackage_UdpCodec &);
  virtual ~DataPackage_UdpCodec();
signals:
public:
  DataPackage_UdpCodec* toAck(QByteArray data=QByteArray());
  QNetworkDatagram * toNetworkDatagram();
  static DataPackage_UdpCodec* fromNetworkDatagram(QNetworkDatagram *qNetworkDatagram);
  uint hashValue();//只计算头部
  static uint hashValue(DataPackage_UdpCodec* dataPackage_UdpCodec);
  static uint hashValue(uint deviceId,uchar packageType,ushort msgId);
private:
  template<class T>static T decode(char* dataPtr,int* dataPtrOffset);
public:
  uint getSenderAddress();
  DataPackage_UdpCodec* setSenderAddress(uint senderAddress);
  ushort getSenderPort();
  DataPackage_UdpCodec* setSenderPort(ushort senderPort);
  uint getReceiverAddress();
  DataPackage_UdpCodec* setReceiverAddress(uint receiverAddress);
  uint getReceiverPort();
  DataPackage_UdpCodec* setReceiverPort(uint receiverPort);
  uint getDeviceId();
  DataPackage_UdpCodec* setDeviceId(uint deviceId);
  uchar getPackageType();
  DataPackage_UdpCodec* setPackageType(uchar packageType);
  ushort getMsgId();
  DataPackage_UdpCodec* setMsgId(ushort msgId);
  ushort getTimeStamp();
  DataPackage_UdpCodec* setTimeStamp(ushort timeStamp);
  QByteArray getData();
  DataPackage_UdpCodec* setData(QByteArray data);
};

#endif // DATAPACKAGEINFO_H
